package com.netty.demo.server.netty.processor;

import com.netty.demo.server.ApplicationUtil;
import com.netty.demo.server.netty.annotation.CommandAnnotation;
import com.netty.demo.server.netty.enums.Command;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Administrator 余秀良
 * @version 1.0
 * <p>date: 2019年09月27日  14:56 </p>
 * <p>address: 四川成都 </p>
 */
@Slf4j
@Component
public class CommandProcessorFactory {
    private static Map<Command, CommandProcessor> commandProcessorMap = new ConcurrentHashMap<>();

    public CommandProcessorFactory() {
        Map<String, Object> beansWithAnnotation = ApplicationUtil.getApplicationContext().getBeansWithAnnotation(CommandAnnotation.class);
        for (Object commandProcessor : beansWithAnnotation.values()) {
            CommandAnnotation annotation = commandProcessor.getClass().getAnnotation(CommandAnnotation.class);
            commandProcessorMap.put(annotation.value(), (CommandProcessor) commandProcessor);
        }
    }

    public static CommandProcessor getCommandProcessor(Command command) {
        return commandProcessorMap.get(command);
    }
}
